package cn.ydxlm.maplib.api;

import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import cn.ydxlm.maplib.entity.Location;

import java.util.ArrayList;
import java.util.List;

public abstract class GetSuggestionResultListener implements PoiSearch.OnPoiSearchListener {
    @Override
    public void onPoiSearched (PoiResult poiResult, int i) {
        final List<PoiItem> allPoi = poiResult.getPois();
        final List<Location> dataList = new ArrayList<>();
        if (allPoi != null) {
            for (PoiItem info : allPoi) {
                final LatLonPoint latLng = info.getLatLonPoint();
                if (latLng == null || latLng.getLatitude() == 0 || latLng.getLongitude() == 0) {
                    continue;
                }
                final Location location = new Location(latLng.getLatitude(), latLng.getLongitude(), Location.SUCCESS, null);
                location.setProvince(info.getProvinceName());
                location.setCity(info.getCityName());
                location.setArea(info.getAdName());
                location.setStreet(info.getSnippet());
                location.setName(info.getTitle());
                dataList.add(location);
            }
        }
        onSuggestionResult(dataList);
    }

    @Override
    public void onPoiItemSearched (PoiItem poiItem, int i) {

    }

    public abstract void onSuggestionResult (List<Location> list);

}
